﻿using SqlSugar;
using ERPSystem.Common.Infrastructure.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ERPSystem.Common.Infrastructure.Repository
{
    /// <summary>
    /// 仓储实现
    /// </summary>
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        private readonly ISqlSugarClient db;
        /// <summary>
        /// 构造函数数据库上下文
        /// </summary>
        /// <param name="db"></param>
        public BaseRepository(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 异步新增
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Add(T entity)
        {
            return await db.Insertable(entity).ExecuteCommandAsync();
        }
        /// <summary>
        /// 批量异步新增
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> AddRange(List<T> entities)
        {
            return db.Insertable(entities).ExecuteCommandAsync();
        }
        /// <summary>
        /// 异步查询
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ISugarQueryable<T>> GetAll()
        {
            return  db.Queryable<T>();
        }

        /// <summary>
        /// 异步修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Update(T entity)
        {
            return await db.Updateable(entity).ExecuteCommandAsync();
        }
        /// <summary>
        /// 异步批量修改
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public Task<int> UpdateRange(List<T> entities)
        {
            return db.Insertable(entities).ExecuteCommandAsync();
        }
    }
}
